<?php
use app\models\Mall;
use yii\helpers\Url;

$web = Url::base();
$institution_code = Mall::getCurrentInstitutionCode();
//单位
$integral_unit = \app\models\Mall::getCurrentInstitutionIntegralUnit();
//比率
$integral_rate = \app\models\Mall::getCurrentInstitutionIntegralRate();
//是否可以混合支付
$is_cash_order = \app\models\Mall::getCurrentInstitutionIsCashOrder();
//调试
$is_cash_order = $institution_code=='JG000001'? 1 :0;
$site_name = \app\models\Mall::getCurrentInstitutionSiteName();

$this->title = $site_name.'->订单确认';
?>
<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall/order-form.css"/>
<script src="<?= $web ?>/js/IDValidator.js"></script>
<div class="location">
    <div class="choice">
        <p class="fl">选择收货地址</p>
<!--        <p class=" new fr">-->
<!--            <a href="user-address?institution_code=--><?//= $institution_code ?><!--">新增收货地址</a>-->
<!--        </p>-->
        <p class=" new fr">
            <a class="new-btn">新增收货地址</a>
        </p>
    </div>
    <div class="site">
        <?php
        if (count($addressList)) {
            foreach($addressList as $item){
        ?>
                <div class="fl select <?=$item['is_default']=='1' ?'cur-bg' :''?>">
                    <p><span name="address_name"><?=$item['name']?></span>收</p>
                    <p name="address_info"><?= $item['province_name'] ?>  <?= $item['city_name'] ?><?= $item['region_name'] ?>  <?= $item['address'] ?></p>
                    <p name="address_phone"><?= $item['phone'] ?></p>
                    <p name="address_remark" style="display: none;"><?= $item['remark'] ?></p>

                </div>
        <?php
            }
        }else{
        ?>
            <p class="address-null">您的收货地址为空，请新增收货地址</p>
        <?php
        }
        ?>

       <!-- <div class="select fl">
            <p><span>李小意</span>收</p>
            <p>重庆市 江北区 海尔路美全22世纪</p>
            <p>185****4149</p>
        </div>
        -->

        <div class="clfl"></div>
    </div>
    <!------------------------->
    <input class="more" type="button" value="更多收货地址" />
    <script type="text/javascript">
        setAddress=function(me){
            me=$(me);
            $('#receiverAddress').html(me.find('[name="address_info"]').html()
                +'    '+me.find('[name="address_name"]').html()+'    '
                +me.find('[name="address_phone"]').html());
            $('#txt_remark').val(me.find('[name="address_remark"]').html());
        };

        $(function() {
            var address_selects=$(".select");
            if(address_selects.length>0){
                setAddress(address_selects.get(0));
            }else {
                $('#receiverAddress').html('');
            }


            $(".select").click(function() {
                setAddress(this);
                $(this).addClass("cur-bg").siblings().removeClass("cur-bg");
            });
            var selectSize = $(".select").size();
            if(selectSize > 3) {
                $(".more").css("display", "block");
                $(".select").eq(2).nextAll(".select").css("display", "none");
            } else {
                $(".more").css("display", "none");
            }
            $(".more").click(function() {
                $(".more").css("display", "none");
                $(".select").eq(2).nextAll(".select").fadeIn(1000);
                $(".select").css("height", "auto").animate(3000);
            });

        });
    </script>
</div>
<!--新建收货地址 begin-->
<div class="nouveau-window" style="display: none;">
    <div class="nouveau fl">
        <a class="close-btn-nouveau"><img src="<?=$web?>/images/mall/close-btn-nouveau.png" /></a>
            <p class="name" >收货姓名：<input type="text" value="" id="txtName" /><span>只包含中文汉字，最多4个字</span></p>
            <p class="phone" >手机号码：<input type="text" value="" id="txtPhone" /></p>
            <div class="info">
                <p class="city fl">选择省市：</p>
                <div class="fl address-list">
                    <select id="s_province" ></select>
                    <select id="s_city" ></select>
                    <select id="s_county"  ></select>
                </div>
                <div class="clfl"></div>
                <div id="show"></div>
            </div>

            <div class="row">
                <p >详细街道：</p>
                <textarea id="txtAddress"></textarea>
            </div>
            <div class="remark">
                <p>备注：</p>
                <textarea id="txtRemark"></textarea>
            </div>
            <input class="submit-btn" id="btn_address_save" type="submit" value="确认提交" />
    </div>
    <div class="nouveau-shade"></div>
</div>
<script type="text/javascript">
    $(function() {
        $(".new-btn").click(function() {
            getProvinceSelect();
            $(".nouveau-window").show();
        });
        $(".close-btn-nouveau").click(function() {
            $(".nouveau-window").hide();
        });
    });
</script>
<script type="application/javascript">
    $(function () {
        $('#s_province').change(function () {
            getCitySelect();
        });

        $('#s_city').change(function () {
            getRegionSelect();
        });

        $('#btn_address_save').click(function () {
            var name = $('#txtName').val().trim();
            if (name.length == 0) {
                alert('请输入姓名');
                $('#txtName').focus();
                return;
            }

            var phone = $('#txtPhone').val().trim();
            if (phone.length == 0) {
                alert('请输入手机号码');
                $('#txtPhone').focus();
                return;
            }

            var provinceId = $('#s_province').val().trim();
            if (provinceId.length == 0) {
                alert('请选择省');
                return;
            }
            var provinceName = $('#s_province').find('option:selected').text().trim();

            var cityId = $('#s_city').val().trim();
            if (cityId.length == 0) {
                alert('请选择市');
                return;
            }
            var cityName = $('#s_city').find('option:selected').text().trim();

            var regionId = $('#s_county').val().trim();
            if (regionId.length == 0) {
                alert('请选择区');
                return;
            }
            var regionName = $('#s_county').find('option:selected').text().trim();

            var address = $('#txtAddress').val().trim();
            if (address.length == 0) {
                alert('请输入详细地址');
                $('#txtAddress').focus();
                return;
            }

            var remark = $('#txtRemark').val().trim();

            var data = {};
            data.address_id = $('#txtAddressId').val();
            data.name = name;
            data.phone = phone;
            data.province_name = provinceName;
            data.city_name = cityName;
            data.region_name = regionName;
            data.address = address;
            data.remark = remark;
            requestJson('save-user-address-handler', data, true, function (responseData) {
                if (responseData.status == 0) {
                    window.location.reload(true);
                } else {
                    alert('保存失败!');
                }
            });
        });
    });

    function getProvinceSelect() {
        $('#s_city').html('');
        $('#s_county').html('');
        requestJson('get-area-handler', null, false, function (responseData) {
            var html ='<option value="">请选择</option>';
            $.each(responseData.data.others,function (i,item) {
                html = html+'<option value="' + item.id + '">' + item.name + '</option>';
            });
            $('#s_province').html(html);
        });
    }

    function getCitySelect() {
        var areaId = $('#s_province').val();
        if (areaId.length == 0) {
            return;
        }
        $('#s_county').html('');
        requestJson('get-area-handler', {parent_id: areaId}, false, function (responseData) {
            var html ='<option value="">请选择</option>';
            $.each(responseData.data.others,function (i,item) {
                html = html+'<option value="' + item.id + '">' + item.name + '</option>';
            });
            $('#s_city').html(html);
        });
    }

    function getRegionSelect() {
        var areaId = $('#s_city').val();
        if (areaId.length == 0) {
            return;
        }
        requestJson('get-area-handler', {parent_id: areaId}, false, function (responseData) {
            var html ='<option value="">请选择</option>';
            $.each(responseData.data.others,function (i,item) {
                html = html+'<option value="' + item.id + '">' + item.name + '</option>';
            });
            $('#s_county').html(html);

        });
    }
</script>
<!--新建收货地址end-->
<div class="affirm-message">
    <div class="title">
        <p>确认订单信息</p>
    </div>
    <div class="list">
        <div class="repertoire">
            <p class="sort"><span class="wares">共<a>（<?=$sum_quantity?>）</a>件商品 </span><span>支付方式</span><span>单价</span><span>数量</span><span>小计</span></p>
        </div>
        <div class="article">
            <?php
            foreach ($cartGoodsList as $item) {
            ?>
                <div class="listing">
                    <div class="goods-img fl">
                        <!---------------------->
                        <a href="goods-detail?goods_code=<?= $item['goods_code'] ?>&institution_code=<?= $institution_code ?>">
                            <img src="<?= $item['goods_image'] ?>@120w"/>
                        </a>
                    </div>
                    <div class="goods-name fl">
                        <p><?php
                            if($item['is_crossborder']=='1'){
                                ?>
                                <span class="cross-title-icon">跨境</span>
                                <?php
                            }
                            ?>
                            <?= $item['name'] ?> </p>
                    </div>
                    <div class="statistics">
                        <ul>
                            <li><?= $integral_unit ?></li>
                            <li><?= intval($item['selling_integral']) ?></li>
                            <li><?= $item['quantity'] ?></li>
                            <li><?= intval($item['selling_integral_sum']) ?></li>
                        </ul>
                    </div>
                </div>
            <?php
            }
            ?>

           <!-- <div class="listing">
                <div class="goods-img fl">
                    <a href=""><img src="imgs/wares1.png" /></a>
                </div>
                <div class="goods-name fl">
                    <p>多功能书籍平板两用折叠支架书籍平板两用折叠支架 </p>
                </div>
                <div class="statistics">
                    <ul>
                        <li>积分</li>
                        <li>289.9</li>
                        <li>1</li>
                        <li>289.9</li>
                    </ul>
                </div>
            </div>-->

        </div>
    </div>
    <?php
    if($is_crossborder=='1'){
    ?>
        <div class="cross-border">
            <div class="massage">
                <p class="massage-1"><span class="name">身份证姓名：</span><input id='txt_idcard_name' type="text" value=""></p>
                <p class="massage-2"> <span class="num">身份证号码：</span><input id="txt_idcard_number" type="text" value=""></p>
            </div>
            <p class="cross-border-title">跨境保税商品购买须知</p>
            <div class="cross-border-content">
                <p class="s-title">跨境保税商品购买须知</p>
                <div class="content-1">
                    <p>一、什么是跨境保税商品<br/>跨境保税商品是采购自国外的商品，进境时海关、国检都会对商品的品质做审查（品质保证），进境后存放于保税区仓库，国内消费者通过电子商务平台下单购买后，在海关全程监管下打包出区，最终交由国内快递送至消费者手上。优点：商品进境时海关、国检对商品审查，商品存放保税仓库（绝无假货，品质保证）</p>
                </div>
                <div class="content-1">
                    <p>二、购买跨境保税商品要求<br /> 1、单笔订单不超过<span>2000元人民币</span>；
                        <br /> 2、每人次一年不能购买超过2万元人民币跨境保税商品（按身份证号计算）；
                        <br /> 3、根据中国海关总署要求，您所购买的商品需提供<span>身份证信息</span>进行入境申报，请您配合。我们不会向第三方泄露您的资料，请您放心。
                    </p>
                </div>
                <div class="content-1">
                    <p>三、跨境商品物流配送<br /> 跨境产品由于海关检验的特殊性，物流送达时间一般时效为：
                        <br /> 保税区邮：
                        <br /> 保税区—圆通，时效<span>5-7天</span>
                        <br /> 香港直邮：
                        <br /> 香港外包仓—申通快递，时效<span>10天左右</span>
                        <br /> 香港汇通仓—汇通快递，时效<span>8-10天</span>
                        <br /> 香港—圆通，时效<span>10-12天</span>
                        <br /> 海外直邮：
                        <br /> 日本直邮—EMS，时效<span>5-7天</span>
                        <br /> 日本—国际段联邦国内转邮政小包或韵达，时效<span>10-13天</span>
                        <br /> 荷兰—中欧快运，时效<span>10-15天</span>
                    </p>
                </div>
                <div class="content-1">
                    <p>
                        <span class="big-red">温馨提示：</span><br /> 由于海关系统升级于<span>16年12月30日18:00</span>后兑换的跨境产品，需要<span>17年1月10日</span>开始发货，给您带来的不变敬请谅解。
                    </p>
                </div>
            </div>

        </div>
    <?php
    }
    ?>


    <div class="verify">
        <div class="verify-message fl">
            <div class="address">
                <p><span>收货信息： </span><span id="receiverAddress"></span></p>
            </div>
            <div class="leave">
                <span>留言备注：</span>
                <input type="text" id="txt_remark"  value="" placeholder="请尽快发货" />
            </div>
        </div>
        <div class="settle fl">
            <div class="integral">
                <p>共<?=$sum_quantity?>件商品</p>
                <p>支付<?=$integral_unit?>：<span><a><?=$sum_integral?></a></span></p>
                <p style="display: none;">可用<?=$integral_unit?>：<span><a><?=intval($userIntegral)?></a></span></p>
                <?php
                $pay_integral = $sum_integral;
                if ($is_cash_order == '1' && $pay_integral > $userIntegral) {
                    $pay_integral = $userIntegral;
                    $pay_cash = round(($sum_integral - $pay_integral) / $integral_rate, 2);
                    ?>
                    <p>支付现金：<span>¥ <a><?= $pay_cash ?></a></span></p>
                    <script>
                        var pay_cash=<?=$pay_cash?>;
                    </script>
                    <?php
                }
                ?>

            </div>
            <div class="pay">
                <input type="button" id="" value="确认并支付" onclick="payExec()"/>
            </div>
        </div>
        <div class="chaining">
            <img src="<?=$web?>/images/mall/chaining.png" />
        </div>
        <div class="chaining2">
            <img src="<?=$web?>/images/mall/chaining.png" />
        </div>
    </div>
</div>

<script>
    var is_crossborder=<?=$is_crossborder?>;
    var selling_price_crossborder=<?=$selling_price_crossborder?>;
    var is_cash_order =<?=$is_cash_order?>;
    payExec = function () {
        //跨境商品超过2000价格，不允许下单
//        alert(selling_price_crossborder);
        if(selling_price_crossborder>2000){
            alert('跨境商品超过2000价格，不允许下单');
            return;
        }
        if(is_crossborder==1){//校验身份证和姓名
            if($('#txt_idcard_name').val()._trim()==''){
                alert('请填写身份证姓名！');
                return;
            }
            var Validator = new IDValidator();
            if(Validator.isValid($('#txt_idcard_number').val())==false){
                alert('请填写正确的身份证号码！');
                return;
            }
        }
        if ($('#receiverAddress').html().length == 0) {
            alert('请填写收货地址！');
            return;
        }
        if (confirm("是否确认支付订单?")) {
//            alert('开始支付');return;
            //支付订单
            waittingShow('等待支付完成');
            var data = {};
            data.remark = $('#txt_remark').val();
            data.idcard_name = $('#txt_idcard_name').val();
            data.idcard_number = $('#txt_idcard_number').val();
            requestJsonMallM('order-submit-handle', data, true, function (re) {
                if (re.status == 0) {
                    var orderSubmitResult = re.data.orderSubmitResult;
                    if (orderSubmitResult.status == '0') {
                        // alert('生成订单成功');
                        window.location.href = "<?=Url::to(['mall/pay-order-success', 'institution_code' => Mall::getCurrentInstitutionCode()])?>" + '&orderId=' + orderSubmitResult.orderId;
                    } else {
                        if (orderSubmitResult.status == '20' && is_cash_order == '1') {
                            waittingHide();
                            //提示是否开始混合支付
                            var cash_integral = orderSubmitResult.cash_integral;
                            invokeCashPay(cash_integral,pay_cash,false,$('#txt_idcard_name').val(),$('#txt_idcard_number').val(),$('#txt_remark').val());
                            return;
                        }
                        alert(orderSubmitResult.message);
                    }

                } else {
                    alert(re.message);
                }
                waittingHide();
             });
        }
    };

</script>
<?=\app\widgets\MallPayCash::widget([])?>
<?=\app\widgets\MallWaitting::widget([])?>